// *************************************
// EFMP Fleet
// EFMP_NNMatch.do
// *************************************


/*** START CODE ***/


// Preamble
// ----------------------------
capture log close
clear all
version 14.2
set more off
set matsize 800


use "${EFMPFleetData}/TransactionData.dta", clear

fillin runquarter zip

foreach i of varlist cntEFMP_* cnt_total { 
	replace `i' = 0 if _fillin == 1
}
gen pre_period_fillin = _fillin if runquarter<23

tempvar maxvar 
bys zip: egen `maxvar' = max(dac)
replace dac = `maxvar' if dac==.

gen logQ = log(cnt_total + 1)

*Save dataset here to retrieve for triple diff after NN Match
save "${EFMPFleetData}/PreNN_forMerge.dta", replace

use "${EFMPFleetData}/PreNN_forMerge.dta", clear

local absorb1 "FE_zip=i.zip DACtimeFE=i.dac##i.runquarter" 

local matchoutcome "${outvarGlobal}"

reghdfe `matchoutcome' , ///
	absorb(`absorb1') vce(cluster zip) resid
predict `matchoutcome'_resid if (runquarter<23), resid
capture drop FE_tract DACtimeFE 
replace `matchoutcome'_resid=. if runquarter>=23

gen `matchoutcome'_pretrend = .
gen `matchoutcome'_pretrend_uncon = .

levelsof zip, local(zips)
	foreach z of local zips {
	
		capture clear matrix
	
		capture qui reg `matchoutcome'_resid runquarter if zip==`z'
		capture matrix b1 = e(b)
		capture replace `matchoutcome'_pretrend = b1[1,1] if zip==`z'
		
		capture qui reg `matchoutcome' runquarter if zip==`z'
		capture matrix b2 = e(b)
		capture replace `matchoutcome'_pretrend_uncon = b2[1,1] if zip==`z'
				
		capture matrix drop b1 b2
		display "`z'"
	}


bys zip: egen `matchoutcome'_pretrend_count = count(`matchoutcome'_resid)
tab `matchoutcome'_pretrend_count
drop `matchoutcome'_resid

cap drop FE_zip
cap drop DACtimeFE


local absorb1 "FE_zip=i.zip DAC_TT=i.dac##i.runquarter" 
local ctrls "" 

reghdfe logQ `ctrls', ///
	absorb(`absorb1') vce(cluster zip)  resid
predict Q_resid if runquarter<23, resid
replace Q_resid=. if runquarter>=23
capture drop FE_Zip DAC_TT

gen Q_pretrend = .
gen Q_pretrend_uncon = .

levelsof zip, local(zips)
	foreach z of local zips {
	
		capture clear matrix
		
		capture qui reg Q_resid runquarter if zip==`z'
		capture matrix b1 = e(b)
		capture replace Q_pretrend = b1[1,1] if zip==`z'
		
		capture qui reg logQ runquarter if zip==`z'
		capture matrix b2 = e(b)
		capture replace Q_pretrend_uncon = b2[1,1] if zip==`z'
		
		capture matrix drop b1 b2
		display "`z'"
	}
	
save "${EFMPFleetData}/PreNN_forMerge2.dta", replace


use "${EFMPFleetData}/PreNN_forMerge2.dta", clear

bys zip: egen Q_pretrend_count = count(Q_resid)

** Collapse dataset to tract in preparation for NN matching / merge

/*Check preperiod fillin frac*/
#delimit ;
collapse (mean) `matchoutcome' pre_period_fillin_frac = pre_period_fillin 
	Q_pretrend_count `matchoutcome'_pretrend_count
	(max) dac aqmd PL225_frac PL300_frac PL400_frac 
	MaxCES `matchoutcome'_pretrend Q_pretrend *_pretrend_uncon
	, by(zip)
;
#delimit cr



** Generate treatment variable and zip labels
gen treat = dac * aqmd

** Save tempfile
tempfile Collapsed
save `Collapsed', replace



save "${EFMPFleetData}/NN_diagnostic_prematch.dta", replace
		
use "${EFMPFleetData}/NN_diagnostic_prematch.dta", clear
	
local NNmatchlist ""
local HardMatchVars "dac"

local NNmatchvars "MaxCES PL*"
teffects nnmatch (`matchoutcome' `NNmatchvars') (aqmd) , ///
	ematch(`HardMatchVars') ate biasadj(`NNmatchvars') nn(1) gen(nn_Demog_) /*os(no_m0)*/ dmv vce(robust)
predict nn_Demog_prox*, distance
local NNmatchlist "`NNmatchlist' Demog"

*Matching on single pretrends
foreach x of varlist `matchoutcome'_pretrend Q_pretrend {
local NNmatchvars "`x'"
teffects nnmatch (`matchoutcome' `NNmatchvars') (aqmd), ///
	ematch(`HardMatchVars') ate biasadj(`NNmatchvars') nn(1) gen(nn_`NNmatchvars'_) dmv vce(robust)
predict nn_`NNmatchvars'_prox*, distance
local NNmatchlist "`NNmatchlist' `x'"
}

*Matching on both price and Q pretrends

local NNmatchvars "Q_pretrend `matchoutcome'_pretrend"
teffects nnmatch (`matchoutcome' `NNmatchvars') (aqmd), ///
	ematch(`HardMatchVars') ate biasadj(`NNmatchvars') nn(1) gen(nn_AllPretrends_) /*os(no_m0)*/ dmv vce(robust)
predict nn_AllPretrends_prox*, distance
local NNmatchlist "`NNmatchlist' AllPretrends"

*Matching on both new and used, price and Q pretrends, and demog

local NNmatchvars "Q_pretrend `matchoutcome'_pretrend MaxCES PL*"
teffects nnmatch (`matchoutcome' `NNmatchvars') (aqmd), ///
	ematch(`HardMatchVars') ate biasadj(`NNmatchvars') nn(1) gen(nn_All_) /*os(no_m0)*/ dmv vce(robust)
predict nn_All_prox*, distance
local NNmatchlist "`NNmatchlist' All"

macro list


save "${EFMPFleetData}/NN_diagnostic_prematchcount.dta", replace

	
use "${EFMPFleetData}/NN_diagnostic_prematchcount.dta", clear
preserve

	foreach i in `matchoutcome'_pretrend Q_pretrend {
		** Generate nearest neighbours by replacing row numbers
		*gen nn1_label = zip[nn1]
		*replace nn1 = zip[nn1]
		rename nn_`i'_1 nn_`i'
		cap rename nn_`i'_prox1 nn_`i'_prox
		gen nn_`i'_label = zip[nn_`i']
		replace nn_`i' = zip[nn_`i']
	}
keep zip nn_`matchoutcome'_pretrend_label nn_Q_pretrend_label 
save "${EFMPFleetData}/NN_zipMatches.dta", replace
restore

foreach i in `NNmatchlist' {
	** Generate nearest neighbours by replacing row numbers
*gen nn1_label = zip[nn1]
*replace nn1 = zip[nn1]
	rename nn_`i'_1 nn_`i'
	cap rename nn_`i'_prox1 nn_`i'_prox
	gen nn_`i'_label = zip[nn_`i']
	replace nn_`i' = zip[nn_`i']

	** Nearest neighbour sample (CHANGED TO RUN OVER AQMD = 1)
	**	(1) treated zips (i.e. AQMD==1)
	gen match_`i' = 1 if aqmd==1
	**	(2) treated zips nearest neighbour
	levelsof nn_`i'_label if aqmd==1, local(matchedzips)
	foreach zip of local matchedzips {
		  tr: replace match_`i' = 1 if inlist(zip, `zip')
	}
	**	(3) eliminate furthest proximity matches
	kdensity nn_`i'_prox if match_`i'==1, lcolor(cranberry) ///
		title("before", box bexpand) scheme(s1manual) ///
		name("ProxDist_`i'_1", replace) nodraw
	cumul nn_`i'_prox if match_`i'==1, equal gen(nn_`i'_cd)
	replace match_`i' = 0 if nn_`i'_cd>.95 & nn_`i'_cd~=.
	replace match_`i' = 0 if nn_`i'_cd==.
	*replace match_`i' = 0 if MaxCES>=60
	kdensity nn_`i'_prox if match_`i'==1, lcolor(cranberry) ///
		title("after", box bexpand) scheme(s1manual) ///
		name("ProxDist_`i'_2", replace) nodraw
	graph combine ProxDist_`i'_1 ProxDist_`i'_2, rows(1) iscale(.5) ///
		xcom ycom title("Proximity density using Match variables: `i'") scheme(s1manual) ///
		name("ProxDist_`i'", replace)
	capture graph export "${EFMPFleetRes}/${ResultsVersion}/ProxDist_`i'.png", replace
	
	replace match_`i' = 0 if match_`i'==.
}

		
tempfile temp
save `temp', replace
keep zip pre_period_fillin_frac
rename zip nn_`matchoutcome'_pretrend_label
rename pre_period_fillin_frac matched_zip_fillin_frac 
merge 1:n nn_`matchoutcome'_pretrend_label using `temp'
keep if _merge~=1
drop _merge
		gen balancedonOutcome = (pre_period_fillin_frac==0 & matched_zip_fillin_frac==0)
		gen match_`matchoutcome'_pretrend_bal = match_`matchoutcome'_pretrend
		gen nn_`matchoutcome'_pretrend_bal_label = nn_`matchoutcome'_pretrend_label
		replace match_`matchoutcome'_pretrend_bal = 0 if balancedonOutcome==0
		replace nn_`matchoutcome'_pretrend_bal_label = . if balancedonOutcome==0
		
local NNmatchlist "`NNmatchlist' `matchoutcome'_pretrend_bal"


		
corr match*
corr match* if treat==0

macro list



foreach i in `NNmatchlist' {
	** Generate zip NN frequency, CHANGED from TREAT =1 to AQMD = 1
	preserve
		keep if match_`i'==1
		bysort nn_`i': egen nn_`i'_count = count(nn_`i')

		collapse (mean) nn_`i'_count, by(nn_`i'_label)
		rename nn_`i'_label zip
		
		** Save tempfile
		tempfile NNdata
		save `NNdata'
	restore

	merge 1:1 zip using `NNdata'
	gen nn_`i'_freq = .
	replace nn_`i'_freq = nn_`i'_count if aqmd==0
	replace nn_`i'_freq = 1 if aqmd==1 & match_`i' == 1
	drop _merge
}

**Generate unmatched frequency list
gen nn_unmatched_freq=1

	save "${EFMPFleetData}/NN_matchoutput.dta", replace

** Retrieve zip level matches
preserve
	keep zip nn_*_freq
	compress
	save "${EFMPFleetData}/NNData_fullAQMDmatch.dta", replace
restore



	
use "${EFMPFleetData}/NN_matchoutput.dta", clear
local diagnosticvar "`matchoutcome'"

keep zip-MaxCES nn_*_pretrend *_pretrend nn_*_prox nn_*_freq 

	tempfile temp temp2
	save `temp', replace
	keep zip dac aqmd `diagnosticvar'_pretrend MaxCES
	rename zip nn_`diagnosticvar'_pretrend
	foreach x in dac aqmd `diagnosticvar'_pretrend MaxCES{
	rename `x' `x'_match
	}
	save `temp2', replace
	use `temp', clear
	merge n:1 nn_`diagnosticvar'_pretrend using `temp2'
	tab aqmd _merge
	keep if _merge ~= 2
	tab aqmd aqmd_match
	tab dac dac_match
	gen trend_diff = `diagnosticvar'_pretrend - `diagnosticvar'_pretrend_match
	sum trend_diff, det
	bys aqmd: sum trend_diff, det
	sort zip
	drop _merge
	
	save "${EFMPFleetData}/NN_diagnostic.dta", replace
	describe


use "${EFMPFleetData}/NN_diagnostic.dta", clear
local NNmatchlist "`matchoutcome'_pretrend Q_pretrend"

foreach x in `NNmatchlist' {
gen `x'_product = `x'*nn_`x'_freq
}
gen `matchoutcome'_pretrend_bal_product = `matchoutcome'_pretrend*nn_`matchoutcome'_pretrend_bal_freq

collapse (sum) *freq *product (mean) `NNmatchlist', by(aqmd dac)

foreach x in `NNmatchlist' `matchoutcome'_pretrend_bal {
gen check_`x' = `x'_product / nn_`x'_freq
}


save "${EFMPFleetData}/NN_diagnosticCollapsed.dta", replace


/*** END CODE ***/

